<template>
  <j-modal
    :title="title"
    :width="1200"
    :visible="visible"
    :confirmLoading="confirmLoading"
    switchFullscreen
    @ok="handleOk"
    @cancel="handleCancel"
    cancelText="关闭"
  >
    <a-spin :spinning="confirmLoading">
      <mapDialog :lat="this.model.lat" :lng="this.model.lng"></mapDialog>
    </a-spin>
  </j-modal>
</template>

<script>
import { httpAction } from '@/api/manage'
import Area from '@/components/_util/Area'
import mapDialog from '../component/map.vue'
import { async } from 'q'
export default {
  name: 'XhdjAgencyModal',
  components: {
    mapDialog,
  },
  data() {
    return {
      title: '操作',
      visible: false,
      previewVisible: false,
      isAdminFlag: false,
      statusFlag: false,
      model: {},
      confirmLoading: false,
      url: {
        add: '/xhdjAgency/add',
        edit: '/xhdjAgency/edit',
      },
    }
  },
  created() {
    this.all = new Area().all
    console.log(this.all);
    this.allList = this.all.filter((res) => res.index == 1)
    this.$bus.$on('maps', (val) => {
      if (val.poi.location.lat) {
        this.model.area = this.cityS(val.poi.adcode).id
        var provincePid = this.cityS(val.poi.adcode).pid
        this.model.city = this.cityS(provincePid).id
        var citypid = this.cityS(provincePid).pid
        this.model.province = this.cityS(citypid).id
        this.model.address = val.poi.address
        this.model.lat = val.poi.location.lat
        this.model.lng = val.poi.location.lng

      } else { 
        this.model.area = this.cityS(val.poi.adcode).id
        var provincePid = this.cityS(val.poi.adcode).pid
        this.model.city = this.cityS(provincePid).id
        var citypid = this.cityS(provincePid).pid
        this.model.province = this.cityS(citypid).id
        this.model.address = val.poi.address
      }
    })
  },
  methods: {
    add() {
      //初始化默认值
      this.edit({})
    },
    // 城市逆向
    cityS(pid) {
      return this.all.find((res) => res.id == pid)
    },
    edit(record) {
      this.model = Object.assign({}, record)
      this.visible = true
    },
    close() {
      this.$emit('close')
      this.visible = false
      // this.$refs.form.clearValidate()
    },
    // 身份上传
    handleCancels() {
      this.previewVisible = false
    },
    async handlePreview(file) {
      if (!file.url && !file.preview) {
        file.preview = await getBase64(file.originFileObj)
      }
      this.previewImage = file.url || file.preview
      this.previewVisible = true
    },
    handleChange({ fileList }) {
      this.fileList = fileList
    },
    handleOk() {
      const that = this
      // 触发表单验证
      that.confirmLoading = true
      let httpurl = ''
      let method = ''
      if (!this.model.id) {
        httpurl += this.url.add
        method = 'post'
      } else {
        httpurl += this.url.edit
        method = 'put'
      }
      httpAction(httpurl, this.model, method)
        .then((res) => {
          if (res.success) {
            that.$message.success(res.message)
            that.$emit('ok')
          } else {
            that.$message.warning(res.message)
          }
        })
        .finally(() => {
          that.confirmLoading = false
          that.close()
        })
    },
    handleCancel() {
      this.close()
    },
  },
}
</script>

<style lang="less" scoped>
.ant-upload-select-picture-card i {
  font-size: 32px;
  color: #999;
}

.ant-upload-select-picture-card .ant-upload-text {
  margin-top: 8px;
  color: #666;
}
</style>
